package vn.mahjonggame.metadata;

import java.util.ArrayList;
import java.util.List;

import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;

public class Tile {
	
	private int id;
	private TileType type;
	private int index;
	
	private boolean isFocus;
	private boolean isJustPick;
	private boolean isHorizontal;
	private boolean isShow;	
	
	private boolean isDora;
	private boolean isInDeathWall;	
	
	public Tile(int id, TileType type){
		this(id, type, 0);
	}
	
	public Tile(int id, TileType type, int index){
		setId(id);
		setType(type);		
		setIndex(index);
	}
		
	public TileType getType() {
		return type;
	}
	public void setType(TileType type) {
		this.type = type;
	}
	public int getIndex() {
		return index;
	}
	public void setIndex(int index) {
		this.index = index;
	}
	
	public boolean isYaochuhai(){		
		return !(this.getIndex() > 1 && this.getIndex() < 9);		
	}
	
	public boolean isJihai(){
		return this.getIndex() == 0;
	}
	
	public boolean isRong(){
		TileType type = this.getType();
		return type == TileType.HAKU || type == TileType.HASTU || type == TileType.CHUN;
	}
	
	public boolean isGio(){
		TileType type = this.getType();
		return type == TileType.TON || type == TileType.NAN || type == TileType.SHA || type == TileType.PEI;
	}
	
	public boolean isInRyuuIisou(){
		
		List<Integer> ryuuIisouIndex = new ArrayList<Integer>();
		ryuuIisouIndex.add(0);
		ryuuIisouIndex.add(2);
		ryuuIisouIndex.add(3);
		ryuuIisouIndex.add(4);
		ryuuIisouIndex.add(6);
		ryuuIisouIndex.add(8);
		
		if(this.getType() != TileType.SOU && this.getType() != TileType.HASTU){			
			return false;
		}
		else {
			if(!ryuuIisouIndex.contains(this.getIndex())){				
				return false;
			}
		}
		
		return true;
	}
	
	public boolean is1Or9(){
		return (this.getIndex() == 1) || (this.getIndex() == 9);
	}
	
	@Override
	public String toString(){
		StringBuffer str = new StringBuffer();
		str.append(this.getId());
		str.append("-");
		str.append(this.getType());
		str.append(this.getIndex()>0 ? this.getIndex() : "");		
		
		return str.toString();
	}
	
	public ISFSObject toSFSObject(){
		ISFSObject sfso = new SFSObject();
        
		sfso.putInt("type", this.getType().ordinal());
        sfso.putInt("index", this.getIndex());
         
        return sfso;
	}
	
	@Override
	public int hashCode() {	    
	    return this.getId();
	}
	
	@Override
	public boolean equals(Object obj) {
	    if (obj == null)
	        return false;
	    
	    if (getClass() != obj.getClass())
	        return false;
	    
	    Tile other = (Tile)obj;	    
	    if(this.getId() == other.getId()) 
	    	return true;
	    
	    return false;
	}

	public boolean isFocus() {
		return isFocus;
	}

	public void setFocus(boolean isFocus) {
		this.isFocus = isFocus;
	}

	public boolean isHorizontal() {
		return isHorizontal;
	}

	public void setHorizontal(boolean isHorizontal) {
		this.isHorizontal = isHorizontal;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public boolean isDora() {
		return isDora;
	}

	public void setDora(boolean isDora) {
		this.isDora = isDora;
	}

	public boolean isInDeathWall() {
		return isInDeathWall;
	}

	public void setInDeathWall(boolean isInDeathWall) {
		this.isInDeathWall = isInDeathWall;
	}

	public boolean isJustPick() {
		return isJustPick;
	}

	public void setJustPick(boolean isJustPick) {
		this.isJustPick = isJustPick;
	}

	public boolean isShow() {
		return isShow;
	}

	public void setShow(boolean isShow) {
		this.isShow = isShow;
	}
}